/**
 *  TextMash - simple IDE for Clojure
 *  
 *  Copyright (C) 2010 Aleksander Naszko
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package pl.olek.textmash;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;

import javax.swing.KeyStroke;
import javax.swing.UIManager;

import pl.olek.textmash.menu.MenuBuilder;


/**
 * 
 * @author anaszko
 *
 */
public class MacOSXSupport extends Support {
	Font font =
		new Font( "Monaco", Font.PLAIN, 12);

	@SuppressWarnings("unchecked")
	public MacOSXSupport()  {
		try {
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", "TextMash");
			System.setProperty("apple.awt.graphics.EnableQ2DX", "true");
			System.setProperty("apple.laf.useScreenMenuBar", "true");	
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			
			
			Class appClass = Class.forName("com.apple.eawt.Application");
			
			Method getApp = appClass.getMethod("getApplication");
			
			Object app = getApp.invoke(null);
			
			Method removeAbout = appClass.getMethod("removeAboutMenuItem");
			
			removeAbout.invoke(app);
			
			Class appListenerClass = Class.forName("com.apple.eawt.ApplicationListener");
			
			Method addListener = appClass.getMethod("addApplicationListener",
					appListenerClass);
			
			Object listener = Proxy.newProxyInstance(MacOSXSupport.class.getClassLoader(), 
					new Class[] { appListenerClass }, new InvocationHandler() {
						
						@Override
						public Object invoke(Object arg0, Method arg1, Object[] arg2)
								throws Throwable {
							String methodName = arg1.getName();
							if (methodName.equals("handleQuit")) {
								pl.olek.textmash.TextMash.terminate();
							} else if (methodName.equals("handleOpenFile")) {
								Method m = arg2[0].getClass().getMethod("getFilename");
								String fileToOpen = (String)m.invoke(arg2[0]);
//								JOptionPane.showConfirmDialog(null, fileToOpen);
								TextMash.actionOpenFile(fileToOpen);
							}
							return null;
						}
					});
			
			addListener.invoke(app, listener);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public Font getFont() {
		return font;
	}

	@Override
	public int positionDialogButton(int amount, int index) {
		return amount - 1 - index;
	}

	@Override
	public KeyStroke getRedoKey() {
		return MenuBuilder.getStdKeyStroke(KeyEvent.VK_Z, MenuBuilder.SHIFT);
	}

	@Override
	public String getTitle(String name) {
		return name;
	}

//	@Override
//	public void decorateWindow(JFrame frame, File file) {
//		JRootPane root = frame.getRootPane( );
//		root.putClientProperty( "Window.documentFile", file );		
//	}

//	@Override
//	public void setModified(JFrame frame, boolean value) {
//		JRootPane root = frame.getRootPane( );
//		root.putClientProperty( "Window.documentModified", new Boolean(value) );
//	}

	@Override
	public URL getResource(String name) {
		return Support.class.getClassLoader(
				).getResource(name);
	}

	@Override
	public String getId() {
		return "mac";
	}

	@Override
	public String getUsername() {
		return System.getenv("USER");
	}

}
